package com.yunshipei.core.manager;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.yunshipei.core.common.ContentValues;
import com.yunshipei.core.common.db.DBManager;
import com.yunshipei.core.model.AdapterApplication;
import com.yunshipei.core.model.UWAApplication;
import com.yunshipei.core.utils.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.yunshipei.core.manager.AdapterPkgHandler.TABLE_ADAPTER_PKG_INFO;
import static com.yunshipei.core.manager.XCloudDataHandler.KEY_ADAPTER_CFG;
import static com.yunshipei.core.manager.XCloudDataHandler.VALUE_ADAPTER_NAME;

/**
 * 处理登录接口dataJS中适配包的信息，如果接口中没有，但是本地存在的适配包均需要删除
 * 小程序也在此处理
 */
class LocalPackageManager {

    private Context context;
    private List<AdapterApplication> adapterApplications;
    private List<UWAApplication> uwaApplications;
    private String rootPath;

    LocalPackageManager(Context context, List<AdapterApplication> adapterApplications, List<UWAApplication> uwaApplications) {
        this.context = context;
        this.adapterApplications = adapterApplications;
        this.uwaApplications = uwaApplications;
        this.rootPath = context.getFilesDir().getPath();
    }

    void updateLocalApps() {
        DBManager dbManager = DBManager.getInstance();
        SQLiteDatabase db = dbManager.openDB(context);
        db.beginTransaction();
        //适配包
        List<String> adapterAppIds = new ArrayList<>();
        if (adapterApplications != null && adapterApplications.size() > 0) {
            for (AdapterApplication adapterApplication : adapterApplications) {
                String appId = adapterApplication.getAppId();
                adapterAppIds.add(appId);
            }
        }
        Cursor cursor1 = db.query(TABLE_ADAPTER_PKG_INFO, new String[]{KEY_ADAPTER_CFG, VALUE_ADAPTER_NAME}, null, null, null, null, null);
        while (cursor1.moveToNext()) {
            String id = cursor1.getString(0);
            if (!TextUtils.isEmpty(id) && !adapterAppIds.contains(id)) {
                db.delete(TABLE_ADAPTER_PKG_INFO, KEY_ADAPTER_CFG + "=?", new String[]{id});
                String scope = cursor1.getString(1);
                if (!TextUtils.isEmpty(scope)) {
                    //删除该适配包
                    FileUtils.deletePathReal(rootPath + File.separator + ContentValues.APP_WEB_ROOT_PATH + File.separator + ContentValues.YSP_NEW_ADAPTER_PAC_PATH + File.separator + scope);
                }
            }
        }
        cursor1.close();
        //小程序
        List<String> uwaAppIds = new ArrayList<>();
        if (uwaApplications != null && uwaApplications.size() > 0) {
            for (UWAApplication uwaApplication : uwaApplications) {
                uwaAppIds.add(uwaApplication.getAppId());
            }
        }
        Cursor cursor2 = db.query(UWAPkgHandler.UWA_INFO_TAB_NAME, new String[]{UWAPkgHandler.UWA_APP_ID, UWAPkgHandler.UWA_PKG_NAME}, null, null, null, null, null);
        while (cursor2.moveToNext()) {
            String id = cursor2.getString(0);
            if (!TextUtils.isEmpty(id) && !uwaAppIds.contains(id)) {
                db.delete(UWAPkgHandler.UWA_INFO_TAB_NAME, UWAPkgHandler.UWA_APP_ID + "=?", new String[]{id});
                String pkgName = cursor2.getString(1);
                if (!TextUtils.isEmpty(pkgName)) {
                    //删除小程序包
                    FileUtils.deletePathReal(rootPath + File.separator + "uwa" + File.separator + pkgName);
                }
            }
        }
        cursor2.close();
        db.setTransactionSuccessful();
        db.endTransaction();
        dbManager.closeDB();
    }
}
